<?php
namespace app\applet\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class Shops extends Model
{
    protected $table = 'shops';
    /**
     * 商铺管理
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsList(){
        //单元楼数据
        $data=Db::name('unit_building')
            ->field('tung_num,unit_num,layer_num,floor_type')
            ->where(['u_type'=>1])
            ->order('tung_num asc,unit_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['tung_num'].'-'.$v['unit_num'];
            }
        }
        //后台朝向数据
        $date=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('housing_oorientation')
            ->find();
        //配套
        $i=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('shop_matching')
            ->find();
        //模板
        $m=Db::name('shops_template')
            ->field('shops_t_id,s_te_name')
            ->where(['s_te_state'=>1])
            ->select();
        //经纪人
        $user=Db::name('admin_user')
            ->where(['admin_status'=>1])
            ->field('admin_id,name')
            ->select();
        //var_dump($m,$date);exit;
        //判断设置没有   没有返回2
        if($data!=null && $date!=null && $i!=null){
            $arr['orientation']=$date['housing_oorientation'];//朝向
            $arr['unit']=$data;//单元
            $arr['matching']=$i['shop_matching'];//配套
            $arr['template']=$m;//模板
            $arr['user']=$user;//模板
            return $arr;
        }else{
            return 2;
        }
    }
    /**
     * 住房房源添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsAdd($data){
        //处理数据
        $arr['s_tung_num'] = isset($data['s_tung_num']) ? trim($data['s_tung_num']) : "";
        $arr['s_unit_num'] = isset($data['s_unit_num']) ? trim($data['s_unit_num']) : "";
        $arr['s_agent'] = isset($data['s_agent']) ? trim($data['s_agent']) : "";
        $arr['s_layer_num'] = isset($data['s_layer_num']) ? trim($data['s_layer_num']) : "";
        $arr['s_room_number'] = isset($data['s_room_number']) ? trim($data['s_room_number']) : "";
        $arr['s_money'] = isset($data['s_money']) ? trim($data['s_money']) : "";
        $arr['s_area'] = isset($data['s_area']) ? trim($data['s_area']) : "";
        $arr['s_face_width'] = isset($data['s_face_width']) ? trim($data['s_face_width']) : "";
        $arr['s_layer_depth'] = isset($data['s_layer_depth']) ? trim($data['s_layer_depth']) : "";
        $arr['s_depth'] = isset($data['s_depth']) ? trim($data['s_depth']) : "";
        $arr['s_orientation'] = isset($data['s_orientation']) ? trim($data['s_orientation']) : "";
        $arr['s_floor_plans'] = isset($data['s_floor_plans']) ? implode(',',$data['s_floor_plans']): "";
        $arr['s_indoor_drawings'] = isset($data['s_indoor_drawings']) ? implode(',',$data['s_indoor_drawings']) : "";
        $arr['s_matching'] = isset($data['s_matching']) ? implode(',',$data['s_matching']) : "";
        $arr['s_a_time'] = time();
        $datas=Db::name('shops')->insert($arr);
        //判断是否存为模板
        $data['s_template']= isset($data['s_template']) ? trim($data['s_template']) : "";
        if($data['s_template']==1){
            //处理数据
            //添加操作用户id
            /*$uid=get_user_id($data['token']);
            $count['te_uid'] = $uid;*/
            $count['s_te_uid'] = 2;
            $count['s_te_name'] = isset($data['s_name']) ? trim($data['s_name']) : "";
            $count['s_te_money'] = isset($data['s_money']) ? trim($data['s_money']) : "";
            $count['s_te_area'] = isset($data['s_area']) ? trim($data['s_area']) : "";
            $count['s_te_face_widt'] = isset($data['s_face_width']) ? trim($data['s_face_width']) : "";
            $count['s_te_layer_depth'] = isset($data['s_layer_depth']) ? trim($data['s_layer_depth']) : "";
            $count['s_te_depth'] = isset($data['s_depth']) ? trim($data['s_depth']) : "";
            $count['s_te_orientation'] = isset($data['s_orientation']) ? trim($data['s_orientation']) : "";
            $count['s_te_floor_plans'] = isset($data['s_floor_plans']) ? implode(',',$data['s_floor_plans']) : "";
            $count['s_te_indoor_drawings'] = isset($data['s_indoor_drawings']) ? implode(',',$data['s_indoor_drawings']) : "";
            $count['s_te_matching'] = isset($data['s_matching']) ? implode(',',$data['s_matching']) : "";
            $count['s_te_state'] = time();
            $data=Db::name('shops_template')->insert($count);
        }
        return $datas;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungs($data){
        //处理数据
        $where['s_tung_num'] = $data['s_tung_num'];
        $where['s_unit_num'] = $data['s_unit_num'];
        $where['s_layer_num'] = $data['s_layer_num'];
        $where['s_room_number'] = $data['s_room_number'];
        $date=Db::name('shops')->where($where)->find();
        return $date;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungsUp($data){
        //处理数据
        $where['s_tung_num'] = $data['s_tung_num'];
        $where['s_unit_num'] = $data['s_unit_num'];
        $where['s_layer_num'] = $data['s_layer_num'];
        $where['s_room_number'] = $data['s_room_number'];
        $wheres['shops_id'] = array('neq',$data['shops_id']);
        $date=Db::name('shops')->where($wheres)->where($where)->find();
        return $date;
    }
    /**
     * 商铺资源查询
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsOne($id){
        //处理数据
        $data=Db::name('shops')
            ->alias('a')
            ->join('admin_user','a.s_agent=admin_user.admin_id')
            ->where(['shops_id'=>$id])
            ->where(['s_state'=>1])
            ->find();
        $data['s_floor_plans']=explode(',',$data['s_floor_plans']);
        $data['s_indoor_drawings']=explode(',',$data['s_indoor_drawings']);
        $data['s_matching']=explode(',',$data['s_matching']);
        return $data;
    }
    /**
     * 商铺修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsUpdate($data){
        //处理数据
        $where['shops_id']=isset($data['shops_id']) ? trim($data['shops_id']) : "";
        $arr['s_tung_num'] = isset($data['s_tung_num']) ? trim($data['s_tung_num']) : "";
        $arr['s_unit_num'] = isset($data['s_unit_num']) ? trim($data['s_unit_num']) : "";
        $arr['s_agent'] = isset($data['s_agent']) ? trim($data['s_agent']) : "";
        $arr['s_layer_num'] = isset($data['s_layer_num']) ? trim($data['s_layer_num']) : "";
        $arr['s_room_number'] = isset($data['s_room_number']) ? trim($data['s_room_number']) : "";
        $arr['s_money'] = isset($data['s_money']) ? trim($data['s_money']) : "";
        $arr['s_area'] = isset($data['s_area']) ? trim($data['s_area']) : "";
        $arr['s_face_width'] = isset($data['s_face_width']) ? trim($data['s_face_width']) : "";
        $arr['s_layer_depth'] = isset($data['s_layer_depth']) ? trim($data['s_layer_depth']) : "";
        $arr['s_depth'] = isset($data['s_depth']) ? trim($data['s_depth']) : "";
        $arr['s_orientation'] = isset($data['s_orientation']) ? trim($data['s_orientation']) : "";
        $arr['s_floor_plans'] = isset($data['s_floor_plans']) ? implode(',',$data['s_floor_plans']) : "";
        $arr['s_indoor_drawings'] = isset($data['s_indoor_drawings']) ? implode(',',$data['s_indoor_drawings']) : "";
        $arr['s_matching'] = isset($data['s_matching']) ? implode(',',$data['s_matching']) : "";
        $arr['s_u_time'] = time();
        $data=Db::name('shops')->where($where)->update($arr);
        return $data;
    }
    /**
     * 商铺显示
     * 1 智能排序， 2 价格从低到高， 3 价格从高到低
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index($order, $house_shape, $page){
        switch ($order) {
            case 1:
                $data = $this->index_intelligent_sorting($house_shape, $page);
                return $data;
            case 3:
                $data = $this->index_price_escalation($house_shape, $page);
                return $data;
            case 2:
                $data = $this->index_price_decline($house_shape, $page);
                return $data;
            default:
                return;
        }
    }
    /**
     * 商铺删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function ShopsDel($id){
        //处理数据
        $where['shops_id'] = $id;
        $date=Db::name('shops')->where($where)->update(['s_state'=>2]);
        return $date;
    }

    public function detail($id) {
        $where = [
            's_state' => '1',
            'shops_id' => $id
        ];
        $data=Db::name($this->table)
            ->field('shops_id, s_tung_num, s_unit_num, s_layer_num, s_money, s_area, s_face_width
            , s_layer_depth, s_depth, s_floor_plans, s_indoor_drawings, s_matching, s_agent,s_room_number,create_id')
            ->where($where)
            ->find();
        return $data;
    }

    /**
     * 智能排序
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function index_intelligent_sorting($house_shape, $page) {
        $where['s.s_state']=1;
        $where['s.lease']=1;
        $where['a.admin_status']=1;
        if (!empty($house_shape)) {
            $house_shape = explode(',', $house_shape);
            $where['s.s_face_width']=$house_shape[0];
            $where['s.s_layer_depth']=$house_shape[1];
            $where['s.s_depth']=$house_shape[2];
        }
        $data=Db::name('shops')
            ->alias('s')
            ->field('s.shops_id,s.s_tung_num,s.s_area,s.s_unit_num,s.s_layer_num,s.s_room_number,s.s_face_width,s.s_money,s.s_layer_depth,s.s_depth,s.s_orientation,s.s_agent,s.s_indoor_drawings, s.s_floor_plans, s.create_id')
            ->page($page, 10)
            ->order('s.s_tung_num asc,s.s_unit_num asc,s.s_room_number asc,s.s_layer_num asc')
            ->join('admin_user a', 's.create_id=a.admin_id')
            ->where($where)
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['s_tung_num'].'-'.$v['s_unit_num'].'-'.$v['s_room_number'];
            $data[$k]['face']='面宽:'.$v['s_face_width'].'层高:'.$v['s_layer_depth'].'进深:'.$v['s_depth'];
        }
        $count=Db::name('shops')
            ->alias('s')
            ->join('admin_user a', 's.create_id=a.admin_id')
            ->where($where)
            ->count();
        $date['data']=$data;
        $date['count']=$count;

        $model = new IntentionalHouse();
        foreach ($date['data'] as $k => $value) {
            $count_info = $model->getOneCount(2, $value['shops_id']);
            $date['data'][$k]['intentional'] = $count_info;
        }

        $last_names = array_column($date['data'],'intentional');
        array_multisort($last_names,SORT_DESC,$date['data']);

        return $date;
    }

    /**
     * 价格从高到低
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function index_price_decline($house_shape, $page) {
        $where['s.s_state']=1;
        $where['s.lease']=1;
        $where['a.admin_status']=1;
        if (!empty($house_shape)) {
            $house_shape = explode(',', $house_shape);
            $where['s.s_face_width']=$house_shape[0];
            $where['s.s_layer_depth']=$house_shape[1];
            $where['s.s_depth']=$house_shape[2];
        }
        $data=Db::name('shops')
            ->alias('s')
            ->field('s.shops_id,s.s_tung_num,s.s_area,s.s_unit_num,s.s_layer_num,s.s_room_number,s.s_face_width,s.s_money,s.s_layer_depth,s.s_depth,s.s_orientation,s.s_agent,s.s_indoor_drawings, s.s_floor_plans, s.create_id')
            ->page($page, 10)
            ->order('s_money desc')
            ->join('admin_user a', 's.create_id=a.admin_id')
            ->where($where)
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['s_tung_num'].'-'.$v['s_unit_num'].'-'.$v['s_room_number'];
            $data[$k]['face']='面宽:'.$v['s_face_width'].'层高:'.$v['s_layer_depth'].'进深:'.$v['s_depth'];
        }
        $count=Db::name('shops')
            ->alias('s')
            ->join('admin_user a', 's.create_id=a.admin_id')
            ->where($where)
            ->count();
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }

    /**
     * 价格从低到高
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function index_price_escalation($house_shape, $page) {
        $where['s.s_state']=1;
        $where['s.lease']=1;
        $where['a.admin_status']=1;
        if (!empty($house_shape)) {
            $house_shape = explode(',', $house_shape);
            $where['s.s_face_width']=$house_shape[0];
            $where['s.s_layer_depth']=$house_shape[1];
            $where['s.s_depth']=$house_shape[2];
        }
        $data=Db::name('shops')
            ->alias('s')
            ->field('s.shops_id,s.s_tung_num,s.s_area,s.s_unit_num,s.s_layer_num,s.s_room_number,s.s_face_width,s.s_money,s.s_layer_depth,s.s_depth,s.s_orientation,s.s_agent,s.s_indoor_drawings, s.s_floor_plans, s.create_id')
            ->page($page, 10)
            ->order('s_money asc')
            ->join('admin_user a', 's.create_id=a.admin_id')
            ->where($where)
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['s_tung_num'].'-'.$v['s_unit_num'].'-'.$v['s_room_number'];
            $data[$k]['face']='面宽:'.$v['s_face_width'].'层高:'.$v['s_layer_depth'].'进深:'.$v['s_depth'];
        }
        $count=Db::name('shops')
            ->alias('s')
            ->join('admin_user a', 's.create_id=a.admin_id')
            ->where($where)
            ->count();
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }
}